<style>
    .filed-name{

        padding: 5px;
        line-height: 30px;
        text-align: right;

    }
    .filed-list{
        width: 100%;
        height:auto;
        overflow: hidden;
        border-bottom: 1px solid #ccc;

    }
    .filed-list:last-child{

        border-bottom: none;

    }
    .filed-input{

        padding: 5px;
        border-left: 1px solid #ccc;
        min-height: 40px;

    }
    .notice{

        font-size: 12px;
        color: #999;
        line-height: 25px;
        width: 100%;
        float: left;
        text-indent: 15px;
    }
    .notice:hover{
        color: #000;
    }
    .tpl-lists{
        margin: 5px;
        border: 1px solid #ccc;
        overflow: hidden;
    }
    .tpl-list{

        width: 100%;
        height: auto;
        padding: 5px;
        border-top: 1px solid #ccc;
        overflow: hidden;

    }
    .tpl-list.default{

        background: #e7e7e7;

    }
    .tpl-list:first-child{

        border-top: none;
    }
    .kc{

        line-height: 30px;
        float: left;
        width: 80px;
        margin-left: 10px;
        text-align: center;
        font-size: 12px;

    }
    .kc.thumb{
        height: 80px;
        line-height: 80px;
        position: relative;
    }
    .kc.thumb .del_thumb{

        width: 20px;
        height: 20px;
        top: 0;
        right: 0;
        background: #fefefe;
        position: absolute;
        text-align: center;
        font-size: 12px;
        line-height: 20px;
        border-top-right-radius: 5px;
        border-bottom-left-radius: 11px;
        color: #999;
        cursor: pointer;
        border: 1px solid #efefef;

    }
    .kc.thumb .del_thumb:hover{

        color: #333;
        background: #efefef;

    }
    .kc.thumb img {
        width: 100%;
        height: 100%;
        border: 1px solid #ddd;
        border-radius: 5px;
        cursor: pointer;
    }
    .kc.thumb img:hover{
        box-shadow: 0 0 3px #bbb;
    }
    .kc.thumb.add-once{
        border: none !important;
    }
    .kc .fa-edit{

        color: #09c;
        cursor: pointer;

    }
    .rlb-no{

        border-left:none !important;
        border-right: none !important;

    }
    .guige > div{

        margin-bottom: 10px;

    }
</style>
<div class="views">
    <div class="view-lists">
        <div class="view-title">
            商品基本信息:
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">

                    商品分类<span class="red">*</span>:

                </div>

                <div class="col-sm-10 filed-input">

                    <div class="col-sm-6">
                        <select name="category_id" disabled="disabled" class="form-control radius-none">
                            <?php echo $category_tree;?>
                        </select>
                    </div>
                    <span class="notice">
                        为防止出现错误数据,商品禁止修改分类;
                    </span>
                </div>
            </div>
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">
                    商品名称<span class="red">*</span>:
                </div>
                <div class="col-sm-10 filed-input">
                    <div class="col-sm-6">
                        <input type="text" value="<?php echo $goods_info['title']?>" name="title" class="form-control radius-none">
                    </div>
                    <span class="notice">
                        商品标题名称长度至少3个字符，最长50个汉字
                    </span>
                </div>
            </div>
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">
                    商品卖点:
                </div>
                <div class="col-sm-10 filed-input">
                    <div class="col-sm-6">
                        <textarea id="description" class="form-control radius-none"><?php echo $goods_info['description']?></textarea>
                    </div>
                    <span class="notice">
                        商品卖点最长不能超过140个汉字
                    </span>
                </div>
            </div>
        </div>
    </div>
    <div class="view-lists">
        <div class="view-title">
            库存设置:
        </div>
        <div class="filed-list">
            <div class="tpl-lists" id="kucun"></div>
        </div>
    </div>
    <div class="view-lists">
        <div class="view-title">
            商品缩略图:
        </div>
        <div class="filed-list">
            <div id="thumb" class="tpl-lists"></div>
        </div>
    </div>
    <div class="view-lists">
        <div class="view-title">
            商品基本信息:
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">

                    图文详情:<span class="red">*</span>:

                </div>

                <div class="pull-left filed-input" style="width: 350px;">
                    <textarea id="content"><?php echo $goods_info['content']?></textarea>
                </div>
            </div>
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">

                    商品参数:

                </div>

                <div class="col-sm-10 filed-input guige">
                    <?php foreach($goods_info['product_data'] as $v):?>
                    <div class="input-group product_data">
                        <span class="input-group-addon radius-none">规格名称:</span>
                        <input type="text" value="<?php echo $v['key']?>" name="name" class="form-control radius-none">
                        <span class="input-group-addon radius-none rlb-no">规格详情:</span>
                        <input type="text" value="<?php echo $v['value']?>" name="key" class="form-control radius-none">
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default btn-danger radius-none guige-remove">
                                <i class="fa fa-remove"></i>
                            </button>
                        </span>
                    </div>
                    <?php endforeach;?>
                    <div class="col-sm-12 text-right">
                        <button class="btn btn-success radius-none add-guige" type="button">添加一组</button>
                    </div>


                </div>
            </div>
        </div>
    </div>
    <div class="view-lists">
        <div class="view-title">
            物流信息:
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">

                    运费模板:

                </div>

                <div class="col-sm-10 filed-input">
                    <div class="col-sm-4">
                        <div class="input-group">
                            <span class="input-group-addon radius-none">设置运费</span>
                            <input type="text" value="<?php echo ($goods_info['goods_freight'] != 0) ? sprintf("%.2f", ($goods_info['goods_freight'] / 100) ) : '0.00'?>" data-type="price" class="form-control radius-none" id="feright" value="0.00">
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="input-group">
                            <input type="text" data-transport_id="<?php echo $transport_info['id']?>" value="<?php echo $transport_info['title']?>" class="form-control radius-none radius-none" id="transport" disabled="disabled">
                            <span class="input-group-btn">
                                <button class="btn btn-default radius-none get-transport" type="button">选择运费模板</button>
                            </span>
                        </div>
                    </div>
                    <span class="notice">
                        如果设置运费模板则优先采用运费模板，如果都没设置,则前台显示包邮
                    </span>
                </div>
            </div>
        </div>
    </div>
    <div class="view-lists">
        <div class="view-title">
            其他信息:
        </div>
        <div class="filed-list">
            <div class="view-content">
                <div class="col-sm-2 filed-name">

                    商品发布:

                </div>

                <div class="col-sm-10 filed-input form-inline">

                    <div class="col-sm-4">
                        <div class="radio">
                            <label>
                                <input type="radio" value="1" checked="checked" name="state"> 立即发布
                            </label>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="radio">
                            <label>
                                <input type="radio" value="2" name="state"> 放入库存
                            </label>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="input-group">
                            <label class="input-group-addon radius-none">
                                <input type="radio" value="3" class="publish_time" name="state">
                                发布时间
                            </label>
                            <input type="text" id="publish_data" class="form-control radius-none" disabled="disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <button type="button" style="width:150px;z-index:9999; position: fixed;bottom:10px;right:50px" class="btn btn-success radius-none" id="save-goods">保存</button>
</div>
<script>


(function($){


        var attr_nums = 0;//动态加载的商品属性数量
        //默认图片上传节点
        var thumb_str = '\
                <div class="tpl-list default"><div class="kc">默认</div></div>\
                <div class="tpl-list _default _data_thumb">';
        <?php foreach($default_spec["slide"] as $k => $v):?>
        <?php if($k == 0):?>
        thumb_str +=  '<div class="kc thumb">\
                            <img class="thumb_upload" src="<?php echo cutUrl($v,80,80)?>" data-src="<?php echo $v?>" alt="">\
                        </div>';
        <?php else:?>

        thumb_str +=  '<div class="kc thumb">\
                        <div title="删除" class="del_thumb"><i class="fa fa-trash"></i></div>\
                            <img class="thumb_upload" src="<?php echo cutUrl($v,80,80)?>" data-src="<?php echo $v?>" alt="">\
                        </div>';

        <?php endif;?>

        <?php endforeach;?>
        thumb_str +='<div class="kc thumb add-once" title="添加一张">\
                        <img src="/statics/common/images/add.png" alt="">\
                    </div>\
                    <span class="notice">\
                        至少上传一张且最多智能上传6张,第一张为默认,上传大小建议为 <span class="red">800x800</span>,大小不超过500kb\
                    </span>\
                </div>';

        //默认库存配置节点
        var tpl_str = '\
            <div class="tpl-list default">\
                <div class="kc">\
                    <span class="red">*</span>\
                    价格(元)\
                </div>\
                <div class="kc">\
                    <span class="red">*</span>\
                    市场价(元)\
                </div>\
                <div class="kc">\
                    <span class="red">*</span>\
                    库存(件)\
                </div>\
                <div class="kc">\
                    <span class="red">*</span>\
                    预警值(件)\
                </div>\
                <div class="kc">\
                    操作\
                </div>\
            </div>\
            <div class="tpl-list _default  _data_tpl">\
                <div class="kc">\
                    <input type="text" value="<?php echo $default_spec["goods_price"] ? sprintf("%.2f",($default_spec["goods_price"] / 100)) : "0.00" ?>" data-type="price" class="form-control radius-none text-center goods_price">\
                </div>\
                <div class="kc">\
                    <input type="text" value="<?php echo $default_spec["goods_market_price"] ? sprintf("%.2f",($default_spec["goods_market_price"] / 100)) : "0.00"?>" data-type="price" class="form-control radius-none text-center goods_market_price">\
                </div>\
                <div class="kc">\
                    <input type="number" value="<?php echo $default_spec["goods_storage"]?>" data-type="int" class="form-control radius-none text-center goods_storage">\
                </div>\
                <div class="kc">\
                    <input type="number" value="<?php echo $default_spec["goods_storage_alarm"]?>" data-type="int" class="form-control radius-none text-center goods_storage_alarm">\
                </div>\
                <div class="kc">\
                <button class="btn btn-danger radius-none" disabled="disabled"><i class="fa fa-trash"></i>删除</button>\
                </div>\
            </div>\
            ';

        //商品的规格数据
        var goods_extend = <?php echo json_encode($goods_extends);?>

        //商品规格参数
        var specs = <?php echo json_encode($goods_info['spec']);?>

        $(function(){

            //获取运费模板
            $('.get-transport').unbind('click');
            $('.get-transport').click(function(){

                var _this = $(this);
                layer.load();

                $.post('<?php echo U("Admin/Goods/Goods/getTransport");?>',function(result){

                    layer.closeAll();
                    layer.open({
                        title:'选择运费模板',
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['80%', '80%'], //宽高
                        content: result,
                        success: function(layero, index){

                            $('.use',layero).unbind('click');
                            $('.use',layero).click(function(){

                                layer.close(index);

                                var transport_id = $(this).data('transport_id');
                                var transport_name = $(this).data('transport_name');

                                _this
                                        .parent()
                                        .prev()
                                        .val(transport_name)
                                        .data('transport_id',transport_id);

                            });

                        }

                    });

                })

            })



            //插入一个库存配置和相对应属性的商品图片
            $('.views').undelegate('input[name="attrs"]','click');
            $('.views').delegate('input[name="attrs"]','click',function(){

                createSpecDom();

            });

            //批量设置
            $('.views').undelegate('.all-set','click');
            $('.views').delegate('.all-set','click',function(){

                var type = $(this).data('type');

                layer.prompt(function(val){

                    if ( type ==  'goods_price' || type == 'goods_market_price') {

                        var val = !isNaN(parseFloat(val).toFixed(2)) ? parseFloat(val).toFixed(2) : '0.00' ;

                        if(val < 0)
                            val = '0.00';

                    } else if( type ==  'goods_storage' || type == 'goods_storage_alarm') {

                        val = Number(val)

                        if(val < 0)
                            val = 0;

                    }

                    $('.'+ type).val(val);

                    layer.closeAll();

                });


            });
            //删除一个库存配置
            $('.views').undelegate('.delete-one-stock','click');
            $('.views').delegate('.delete-one-stock','click',function(){



                var _this = $(this);

                layer.msg('确定删除吗?',{
                    time: 0,
                    btn:['删除','关闭'],
                    yes:function(){

                        layer.closeAll();

                        _this
                                .parent()
                                .parent()
                                .fadeOut(200,function(){


                                    var id = $(this).data('id');

                                    $('#'+id).remove();

                                    $(this).remove();

                                    if($('#kucun > .tpl-list:not(".default")').length < 1){
                                        $('#kucun').html(tpl_str);
                                        $('#thumb').html(thumb_str);
                                    }

                                })


                    }

                });

            });

            //添加一张缩略图
            $('#thumb').undelegate('.add-once','click');
            $('#thumb').delegate('.add-once','click',function(){


                $(this).before('\
                        <div class="kc thumb">\
                            <div class="del_thumb" title="删除"><i class="fa fa-trash"></i></div>\
                            <img class="thumb_upload" src="/statics/common/images/image.png" alt="">\
                        </div>\
                ');

                if($(this).siblings('.thumb').not('.add-once').length >= 6){

                    $(this).hide();

                }

            });

            //删除缩略图
            $('#thumb').undelegate('.del_thumb','click');
            $('#thumb').delegate('.del_thumb','click',function(){

                var _this = $(this);

                if($('.thumb',_this.parent().parent()).length < 2){

                    layer.msg('至少上传一张缩略图',{shift:6});

                    return false;
                }
                layer.msg('确定删除吗?',{
                    time: 0,
                    btn:['删除','关闭'],
                    yes:function(){

                        layer.closeAll();

                        _this
                                .parent()
                                .fadeOut(200,function(){
                                    $('.add-once',$(this).parent().parent()).show();
                                    $(this).remove();


                                })

                    }

                });

            });



            //选择商品属性动态生成库存配置
//        $('.views').undelegate('input[name="attrs"]','click');
//        $('.views').delegate('input[name="attrs"]','click',function(){
//
//            var _this = $(this);
//
//        });
            //初始化商品属性
            getAttrs($('select[name="category_id"]').val());
            //动态加载商品属性
            $('select[name="category_id"]').change(function(){


                var category_id = $(this).val();

                getAttrs(category_id);

            });
            //设置时间选择控件皮肤
            laydate({
                elem: '#publish_data',
                format: 'YYYY-MM-DD hh:mm:ss', // 分隔符可以任意定义，该例子表示只显示年月
                festival: true,
                event: 'focus',
                min: Admin.getTime('-'), //设定最小日期为当前日期
                max: '2099-06-16 23:59:59', //最大日期
                istime: true
            });

            //商品发布时间状态
            $('input[name="state"]').click(function(){

                if($(this).hasClass('publish_time')){

                    $('#publish_data')
                            .removeAttr('disabled');
                }else{


                    $('#publish_data')
                            .attr('disabled','disabled')
                            .val('');
                }
            });

            //添加一组规格
            $('.add-guige').unbind('click');
            $('.add-guige').click(function(){

                var random1 = Math.random();
                var random2 = Math.random();

                $(this)
                        .parent()
                        .before('\
                    <div class="input-group product_data">\
                        <span class="input-group-addon radius-none" id="id_'+random1+'">规格名称:</span>\
                        <input type="text" class="form-control radius-none" name="name" aria-describedby="id_'+random1+'">\
                        <span class="input-group-addon radius-none rlb-no" id="id_'+random2+'">规格详情:</span>\
                        <input type="text" class="form-control radius-none" name="key" aria-describedby="id_'+random2+'">\
                        <span class="input-group-btn">\
                            <button class="btn btn-default btn-danger radius-none guige-remove" type="button"><i class="fa fa-remove"></i></button>\
                        </span>\
                    </div>\
                    ');

            });

            //删除一个规格
            $('.guige').undelegate('.guige-remove','click');
            $('.guige').delegate('.guige-remove','click',function(){

                var _this = $(this);

                layer.msg('确定删除吗?',{
                    time: 0,
                    btn:['删除','关闭'],
                    yes:function(){

                        layer.closeAll();

                        _this
                                .parent()
                                .parent()
                                .fadeOut(200,function(){

                                    $(this).remove();

                                })

                    }

                });

            });

            //规范表单里面的数据
            $('.views').undelegate('input','blur');
            $('.views').delegate('input','blur',function(){

                var type = $(this).data('type');

                var value = $(this).val();

                if(type == 'int' ){

                    if(Number(value) > 0){

                        $(this).val(Number(value))

                    } else {

                        $(this).val(0)

                    }

                }else if(type == 'price'){

                    var val = !isNaN(parseFloat(value).toFixed(2)) ? parseFloat(value).toFixed(2) : '0.00' ;

                    if(val > 0){

                        $(this).val(val);

                    } else {

                        $(this).val('0.00');

                    }

                }
            });



        });

        //保存商品
        $(function(){

            $('#main-content').undelegate('#save-goods','click');
            $('#main-content').delegate('#save-goods','click',function(){

                var data = {};
                data.basic = {};
                /**************************获取基础数据****************************/

                    //获取分类ID
                data.basic.category_id = $('select[name="category_id"]').val();
                //获取标题
                data.basic.title = $('input[name="title"]').val();
                if(data.basic.title.length < 3)
                {

                    showMessage('标题至少三个字符以上!',300,'','',2000);
                    return false;

                }
                //获取运费
                data.basic.goods_freight = $('#feright').val();
                //获取运费模板ID

                data.basic.transport_id = $('#transport').data('transport_id');
                //获取描述(卖点)

                data.basic.description = $('#description').val();

                if(data.basic.description.length > 140){

                    showMessage('商品买点最多不能超过140个字符!',300,'','',2000);
                    return false;

                }

                //获取商品描述
                data.basic.content = ueditor.getContent();

                if(!data.basic.content){

                    showMessage('请输入商品描述!',300,'','',2000);
                    return false;

                }
                //获取商品规格
                data.basic.spec = {};

                $('#kucun .tpl-list.default .attr_val').each(function(i){

                    var _this = $(this);

                    data.basic.spec[i] = {};
                    data.basic.spec[i].attr = {};
                    data.basic.spec[i].attr_val = {};
                    data.basic.spec[i].attr.attr_id = _this.data('attr_id');
                    data.basic.spec[i].attr.attr_name = _this.text();

                });

                //获取商品规格
                $('#kucun ._data_tpl').each(function(index){

                    var _this = $(this);
                    $('.attr_val',_this).each(function(i){

                        var attr_id = $(this).data('attr_id');
                        var attr_val_id = $(this).data('attr_val_id');
                        var attr_val_name = $(this).text();


                        if(data.basic.spec[i].attr.attr_id == attr_id){

                            data.basic.spec[i].attr_val[index] = {};
                            data.basic.spec[i].attr_val[index] = {};
                            data.basic.spec[i].attr_val[index].attr_val_id = attr_val_id;
                            data.basic.spec[i].attr_val[index].attr_val_name = attr_val_name;

                        }
                    });

                });


                //获取商品发布状态
                var goods_state = $('input[name="state"]:checked').val();

                switch (Number(goods_state))
                {

                    case 1:


                        //立即发布
                        data.basic.goods_state = 1;

                        data.basic.publish_time = Admin.getTime('-');


                        break;

                    case 2:

                        //放入库存(下架状态)
                        data.basic.goods_state = 0;

                        data.basic.publish_time = Admin.getTime('-');

                        break;

                    case 3:

                        //立即发布
                        data.basic.goods_state = 1;

                        data.basic.publish_time = $('#publish_data').val() ? $('#publish_data').val() : Admin.getTime('-') ;

                        break;

                }

                //获取商品参数
                data.basic.product_data = {};

                if($('.product_data').length > 0){

                    $('.product_data').each(function(i,v){

                        var _this = $(this);

                        data.basic.product_data[i] = {};
                        var product_data_name = $('input[name="name"]',_this).val();
                        var product_data_key = $('input[name="key"]',_this).val();

                        data.basic.product_data[i].key = product_data_name;
                        data.basic.product_data[i].value = product_data_key;

                    });

                }


                /***********获取商品库存和商品缩略图**********/
                data.extend = {};
                //获取商品规格及规格所对应的价格
                $('#kucun ._data_tpl').each(function(i){

                    var _this = $(this);

                    data.extend[i] = {}
                    data.extend[i].goods_attr_val_ids = '';
                    data.extend[i].is_default = '0';

                    if(!i){
                        data.extend[i].is_default = 1;
                        data.basic.goods_price = $('.goods_price',_this).val();
                        data.basic.goods_market_price = $('.goods_market_price',_this).val();
                    }


                    var goods_attr_val_ids = '';

                    if($('.attr_val',_this).length > 0){

                        $('.attr_val',_this).each(function(i){

                            goods_attr_val_ids += $(this).data('attr_val_id')+',';

                        });

                        if( goods_attr_val_ids )
                            goods_attr_val_ids = goods_attr_val_ids.substr(0,goods_attr_val_ids.length -1)

                    }

                    data.extend[i].goods_attr_val_ids = goods_attr_val_ids;

                    //获取规格ID
                    if (goods_extend && goods_extend[goods_attr_val_ids] ) {

                        data.extend[i].id = goods_extend[goods_attr_val_ids].id;

                    }

                    //获取当前配置的商品价格
                    data.extend[i].goods_price = $('.goods_price',_this).val();
                    //获取市场价格
                    data.extend[i].goods_market_price = $('.goods_market_price',_this).val();
                    //获取商品库存
                    data.extend[i].goods_storage = $('.goods_storage',_this).val();
                    //获取库存预警值
                    data.extend[i].goods_storage_alarm = $('.goods_storage_alarm',_this).val();

                });

                //获取商品规格对应的商品图片
                $('#thumb ._data_thumb').each(function(i){

                    var _this = $(this);
                    data.extend[i].slide = [];

                    $('.kc.thumb:not(".add-once")>img',_this).each(function(index){

                        //获取商品默认缩略图
                        if(!index && !i)
                            data.basic.thumb = $(this).data('src');

                        if(!index)
                            data.extend[i].thumb = $(this).data('src');

                        data.extend[i].slide.push($(this).data('src')) ;

                    });


                });

                layer.load();
                $.post('<?php echo U("Admin/Goods/Goods/update");?>',{'data':data,'id':<?php echo $id;?>},function(result){

                layer.closeAll();

                showMessage(result.msg,result.code,false,false,2000);


            })

        });

    });



    //动态加载商品属性
    function getAttrs(category_id)
    {

        $('#attrs').remove();
        if(!category_id)
            return false;

        $.post("<?php echo U('Admin/Goods/Goods/getAttrs')?>",{'category_id':category_id},function(result){


            //初始化库库存配置
            $('#kucun').html(tpl_str);
            $('#thumb').html(thumb_str);


            if(typeof result == 'object')
                return false;

            $('.view-lists:first-child').after(result);

            attr_nums = $('.view-lists .attrs').length;


            //加载成功之后初始化规格选中

            for ( var i in specs ) {


                for ( var idx in specs[i].attr_val ) {
                    var id = specs[i].attr_val[idx].attr_val_id;


                    $('#attr_'+id).prop('checked',true)
                            .attr('disabled','disabled')
                            .parent()
                            .parent()
                            .css('opacity',0.7);

                }

            }

            createSpecDom();

        });

    }

    function createSpecDom()
    {



        var tpl_list = '<div class="tpl-list default">';

        var _thumb = '<div class="tpl-list default">';

        var $spec = [];

        $('.view-lists .attrs').each(function(){

            if($('input[name="attrs"]:checked',$(this)).length > 0)
                $spec.push($(this));


        })

        if($spec.length < 1){
            //如果什么都没有选择,则回复默认

            $('#kucun').html(tpl_str);
            $('#thumb').html(thumb_str);
            return;

        }


        for ( var i in $spec ) {

            //生成库存设置头部状态栏
            var $filed_name = $('.filed-name',$spec[i]);

            //head_status += '<div class="kc" data-id="'+ $filed_name.data('id') +'">' + $filed_name.data('name') + '</div>';
            tpl_list += '<div class="kc attr_name attr_val" data-attr_id="'+ $filed_name.data('id') +'">'+ $filed_name.data('name') +'</div>';

            _thumb += '<div class="kc attr_name attr_val" data-attr_id="'+ $filed_name.data('id') +'">'+ $filed_name.data('name') +'</div>';


        }

        _thumb += '<div class="kc">默认</div></div>';

        tpl_list += '<div class="kc">\
                                <span class="red">*</span>\
                                价格(元)<i data-toggle="tooltip" data-placement="top" title="批量设置" class="fa fa-edit all-set" data-type="goods_price"></i>\
                            </div>\
                            <div class="kc">\
                                <span class="red">*</span>\
                                市场价(元)<i data-toggle="tooltip" data-placement="top" title="批量设置" class="fa fa-edit all-set" data-type="goods_market_price"></i>\
                            </div>\
                            <div class="kc">\
                                <span class="red">*</span>\
                                库存(件)<i data-toggle="tooltip" data-placement="top" title="批量设置" class="fa fa-edit all-set" data-type="goods_storage"></i>\
                            </div>\
                            <div class="kc">\
                                <span class="red">*</span>\
                                预警值(件)<i data-toggle="tooltip" data-placement="top" title="批量设置" class="fa fa-edit all-set" data-type="goods_storage_alarm"></i>\
                            </div>\
                            <div class="kc">\
                                操作\
                            </div>\
                        </div>\
                        ';

        var queue = [];
        $('.filed-list.attrs').each(function () {
            var ary = [];
            $(this).find('input:checked').each(function () {
                //ary.push($(this).val());
                ary.push($(this).attr('data-name')+','+$(this).val()+','+$(this).data('attr_id'));
            });
            queue.push(ary);
        });

        function each (a, b) {
            var out = [];

            if (0 == a.length) { return b; }
            if (0 == b.length) { return a; }

            for(var n = 0; n < a.length; n++) {
                for (var m = 0; m < b.length; m++) {
                    out.push(a[n] + '_' + b[m]);
                }
            }

            return out;
        }

        var res = queue.shift();

        while (queue.length > 0) {

            res = each(res, queue.shift());

        }

        for (var i in res) {

            var row = res[i].split('_');

            var id = 'id_'+Math.round(Math.random() * 10000);
            //库存配置节点
            tpl_list += '<div data-id='+ id +' class="tpl-list _data_tpl">';
            _thumb += '<div id='+ id +' class="tpl-list _data_thumb">';

            var specs_str = '';

            for ( var idx in row ) {

                var info = row[idx].split(',');

                tpl_list += '<div class="kc attr_val" data-attr_val_id="'+ info[1] +'" data-attr_id="'+ info[2] +'">'+ info[0] +'</div>';
                _thumb += '<div class="kc attr_val" data-attr_val_id="'+ info[1] +'" data-attr_id="'+info[2]+'" style="line-height: 80px">'+ info[0] +'</div>';

                specs_str += info[1]+',';

            }

            specs_str = specs_str.substring(0,specs_str.length-1);

            if (goods_extend && goods_extend[specs_str] ) {



                tpl_list += '\
                        <div class="kc">\
                            <input type="text" value="'+ (goods_extend[specs_str].goods_price / 100).toFixed(2) +'" data-type="price" class="form-control radius-none text-center goods_price">\
                        </div>\
                        <div class="kc">\
                            <input type="text" value="'+ (goods_extend[specs_str].goods_market_price / 100).toFixed(2) +'" data-type="price" class="form-control radius-none text-center goods_market_price">\
                        </div>\
                        <div class="kc">\
                            <input type="number" value="'+ goods_extend[specs_str].goods_storage +'" data-type="int" class="form-control radius-none text-center goods_storage">\
                        </div>\
                        <div class="kc">\
                            <input type="number" value="'+ goods_extend[specs_str].goods_storage_alarm +'" data-type="int" class="form-control radius-none text-center goods_storage_alarm">\
                        </div>\
                        <div class="kc">\
                        <button class="btn btn-danger radius-none delete-one-stock" disabled="disabled"><i class="fa fa-trash"></i>删除</button>\
                        </div>\
                    </div>';


                if ( goods_extend[specs_str].slide ) {

                    for (var i in goods_extend[specs_str].slide){

                        if( i == 0 ) {

                            _thumb += '<div class="kc thumb">\
                                            <img class="thumb_upload" src="'+ goods_extend[specs_str].slide[i] +'" data-src="'+ goods_extend[specs_str].slide[i] +'" alt="">\
                                        </div>';

                        } else {

                            _thumb += '<div class="kc thumb">\
                                        <div class="del_thumb" title="删除">\
                                            <i class="fa fa-trash"></i>\
                                        </div>\
                                        <img class="thumb_upload" src="'+ goods_extend[specs_str].slide[i] +'" data-src="'+ goods_extend[specs_str].slide[i] +'" alt="">\
                                    </div>';

                        }

                    }
                    if ( goods_extend[specs_str].slide.length < 6 ) {

                        _thumb += '<div class="kc thumb add-once" title="添加一张">\
                            <img src="/statics/common/images/add.png" alt="">\
                        </div>';

                    }

                } else {

                    _thumb += '<div class="kc thumb">\
                                    <img class="thumb_upload" src="/statics/common/images/image.png" alt="">\
                                </div>\
                                <div class="kc thumb add-once" title="添加一张">\
                                    <img src="/statics/common/images/add.png" alt="">\
                                </div>';

                }

                _thumb += '<span class="notice">\
                                至少上传一张且最多智能上传6张,第一张为默认,上传大小建议为 <span class="red">800x800</span>,大小不超过500kb\
                                </span>\
                            </div>';


            } else {

                tpl_list += '\
                        <div class="kc">\
                            <input type="text" value="1.00" data-type="price" class="form-control radius-none text-center goods_price">\
                        </div>\
                        <div class="kc">\
                            <input type="text" value="1.00" data-type="price" class="form-control radius-none text-center goods_market_price">\
                        </div>\
                        <div class="kc">\
                            <input type="number" value="1" data-type="int" class="form-control radius-none text-center goods_storage">\
                        </div>\
                        <div class="kc">\
                            <input type="number" value="1" data-type="int" class="form-control radius-none text-center goods_storage_alarm">\
                        </div>\
                        <div class="kc">\
                        <button class="btn btn-danger radius-none delete-one-stock"><i class="fa fa-trash"></i>删除</button>\
                        </div>\
                    </div>';

                _thumb += '\
                        <div class="kc thumb">\
                            <img class="thumb_upload" src="/statics/common/images/image.png" alt="">\
                        </div>\
                        <div class="kc thumb add-once" title="添加一张">\
                            <img src="/statics/common/images/add.png" alt="">\
                        </div>\
                        <span class="notice">\
                        至少上传一张且最多智能上传6张,第一张为默认,上传大小建议为 <span class="red">800x800</span>,大小不超过500kb\
                        </span>\
                    </div>';

            }

        }


        //生成一个商品库存配置
        $('#kucun.tpl-lists').html(tpl_list);
        $('#thumb.tpl-lists').html(_thumb);
        $('[data-toggle="tooltip"]').tooltip();


    }
})($);
    var ueditor;
    //加载百度编辑器
    Admin.getUeditor('content',function(ue){

        ueditor = ue;

    });
    //图像上传事件触发节点
    var thumb_upload;

    //图像上传
    Admin.upload('.thumb_upload',false,function(_this){

        thumb_upload = _this;

    });
    //图片上传回调
    function getImageSrc(_this){

        thumb_upload.attr('src',cutUrl(_this[0],80,80));
        thumb_upload.attr('data-src',_this[0]);

    }
</script>